package com.sparsearr.datastructures.ztr.search;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/6/1 20:41
 * @Description: 线性查找
 * @Version: 1.0
 */

public class SeqSearch {
    private static final Logger logger = LogManager.getLogger(SeqSearch.class);

    public static void main(String[] args) {
        int[] arr = {1, 9, 11, -1, 34, 89};
        int index = seqSearch(arr, -11);
        if (index == -1) {
            logger.info("没有找到");
        } else {
            logger.info("找到，下标为：{}", index);
        }
    }

    /**
     * 线性查找
     *
     * @param arr   数组
     * @param value 要查找的值
     * @return 如果找到，就返回下标，如果没有找到，就返回-1
     */
    public static int seqSearch(int[] arr, int value) {
        // 线性查找是逐一比对，发现匹配后，就返回下标
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == value) {
                return i;
            }
        }
        return -1;
    }
}
